/*----------------------------------------------------------------------------*\
| /gamemode/cl_inventory.lua           | inventory system                | CLI |
\*----------------------------------------------------------------------------*/

-- Recode of my inventory
-- Define Inventory table.
INV = {}

-- Some basic configuration.
INV.Conf = {}
INV.Conf.NumObjects 		= 20 	-- The number of objects we can hold.

surface.CreateFont( "Tahoma", 20, 250, true, false, "InventoryFont" )
surface.CreateFont( "Tahoma", 30, 400, true, false, "InventoryFontSuperBig" )

function INV.Init()
	-- We only have the one objects table on the client.
	INV.Objects 		= {}
	for i = 1, INV.Conf.NumObjects do	-- Lets do this properly this time, with 1 indexed tables.
		INV.Objects[i] = {}
		INV.ResetObject( i )
	end
	
	-- Initalize the GUI
	INV.InitGUI()
end
hook.Add( "Initialize", "INV.Init", INV.Init );  
	
function INV.ResetObject( id )
	local obj = INV.Objects[id]
	obj.InUse		= false
	obj.Entity 		= nil
	obj.Weight		= 0
	obj.Name		= 0
	obj.Model		= 0
end 

-- Receive usermessages here.
function INV.Update( um )
	local id = um:ReadShort()
	local obj = INV.Objects[id]
	
	obj.InUse	= um:ReadBool()
	obj.Weight	= um:ReadShort()
	obj.Name	= um:ReadString()
	obj.Model	= um:ReadString()
	
	obj.Panel:Update( obj.InUse, id, obj.Weight, obj.Name, obj.Model )
end 
usermessage.Hook( "InventoryUpdate", INV.Update )

--[[-------------------------------------------------------------------------------------------------
 VGUI attempt starts here!
-------------------------------------------------------------------------------------------------]]--

--[[-------------------------------------------------------------------------------------------------
 SpawnIcon override.
-------------------------------------------------------------------------------------------------]]--
local SpawnIconOverride = {}

function SpawnIconOverride:OnMousePressed( mc )
	return true
end

function SpawnIconOverride:OnMouseOver( mc )
	return true
end
vgui.Register( "SpawnIconOVR", SpawnIconOverride, "SpawnIcon" )

--[[-------------------------------------------------------------------------------------------------
 The panel created for each object.
-------------------------------------------------------------------------------------------------]]--
local ObjectPANEL = {}

ObjectPANEL.Slot = nil

function ObjectPANEL:Init()	

	self.lblInUse = vgui.Create( "DLabel", self )
		self.lblInUse:SetPos( 125, 20 )
		self.lblInUse:SetSize( 250, 30 )
		self.lblInUse:SetText( "NOTHING HERE!" )
		self.lblInUse:SetSize( 312, 20 )
		self.lblInUse:SetTextColor( Color( 10, 10, 10, 200 ) )
		self.lblInUse:SetFont( "InventoryFontSuperBig" )
		self.lblInUse:SetVisible( true )

	self.Icon = vgui.Create( "SpawnIconOVR", self )
		self.Icon:SetPos( 3, 3 )
		self.Icon:SetSize( 64, 64 )
		
	self.lblName = vgui.Create( "DLabel", self )
		self.lblName:SetPos( 70, 2 )
		self.lblName:SetSize( 312, 20 )
		self.lblName:SetTextColor( Color( 10, 10, 10, 200 ) )
		self.lblName:SetFont( "InventoryFont" )
		
	self.lblWeight = vgui.Create( "DLabel", self )
		self.lblWeight:SetPos( 70, 24 )
		self.lblWeight:SetSize( 312, 20 )
		self.lblWeight:SetTextColor( Color( 10, 10, 10, 200 ) )
		self.lblWeight:SetFont( "InventoryFont" )
		
	self.btnDrop = vgui.Create( "DButton", self )
		self.btnDrop:SetPos( 383, 5 )
		self.btnDrop:SetSize( 64, 28 )
		self.btnDrop:SetText( "DROP" )
		self.btnDrop.DoClick =	function( btn )
									//print( "Drop function running!" )
									//print( tostring( self.Slot ) )
									if self.Slot then
										//print( "Running drop command " .. self.Slot )
										LocalPlayer():ConCommand( "~INV_DROP " .. self.Slot )
										--RunConsoleCommand( "~INV_DROP", self.Slot ) For when the beta is released.
									else
										-- Do something here probably.
									end
								end
		
	self.btnUse = vgui.Create( "DButton", self )
		self.btnUse:SetPos( 383, 35 )
		self.btnUse:SetSize( 64, 28 )
		self.btnUse:SetText( "USE" )
		self.btnUse.DoClick =	function( btn )
									if self.Slot then
										LocalPlayer():ConCommand( "~INV_USE " .. self.Slot )
										--RunConsoleCommand( "~INV_DROP", self.Slot ) For when the beta is released.
									else
										-- Do something here probably.
									end
								end	

	-- To start with everything is hidden, because there is nothing there! 
	self.InUse = false
	self.lblInUse:SetVisible( true )
	self.Icon:SetVisible( false )
	self.lblWeight:SetVisible( false )
	self.lblName:SetVisible( false )	
	self.btnDrop:SetVisible( false )
	self.btnUse:SetVisible( false )
end

function ObjectPANEL:Update( inuse, slot, weight, name, model )
	if inuse then
		self.InUse	= true
		
		//print( slot )
		self.Slot	= slot
		//print( self.Slot )
		
		self.lblInUse:SetVisible( false )
		
		self.Icon:SetVisible( true )
		self.Icon:SetModel( model )
		
		self.lblWeight:SetVisible( true )
		self.lblWeight:SetText( "Weight: " .. tostring( weight ) )
		
		self.lblName:SetVisible( true )
		self.lblName:SetText( "Name: " .. name )
		
		self.btnDrop:SetVisible( true )
		self.btnUse:SetVisible( true )
	else
		self.InUse = false
		self.lblInUse:SetVisible( true )
		self.Icon:SetVisible( false )
		self.lblWeight:SetVisible( false )
		self.lblName:SetVisible( false )
		self.btnDrop:SetVisible( false )
		self.btnUse:SetVisible( false )
	end
end

function ObjectPANEL:Paint()
	-- Draw the background.
	local w, h = self:GetSize()
	draw.RoundedBox( 4, 0, 0, w, h, Color( 255, 255, 255, 200 ) )
end

vgui.Register( "INVObject", ObjectPANEL, "Panel" )

function INV.InitGUI()

	-- Store some stuff locally to save function calls.
	local ply = LocalPlayer()
	
	local width		= 506 -- formerly 480 but dpanellist raped my sizes
	local height	= 480

	INV.Window = vgui.Create( "DFrame" )
		INV.Window:SetSize( width, height )
		INV.Window:Center()
		INV.Window:SetTitle( "Inventory GUI 0.1" )
		INV.Window:SetVisible( false )
		INV.Window:SetDeleteOnClose( false )
		INV.Window:SetBackgroundBlur( true )
		INV.Window:SetDraggable(false)
		INV.Window:ShowCloseButton(false)
		INV.Window:MakePopup()
		
	INV.List = vgui.Create( "DPanelList", INV.Window )
		INV.List:EnableVerticalScrollbar()
		INV.List:SetSize( width - 20, height - 100 )
		INV.List:SetPos( 10, 30 )
		INV.List:SetSpacing( 10 )
		INV.List:SetPadding( 10 )
		
		for i = 1, #INV.Objects do
			local obj = INV.Objects[i]
			local y = ( i * 70 ) + 10
			
			obj.Panel = vgui.Create( "INVObject", INV.List )
				obj.Panel:SetPos( 10, y )
				obj.Panel:SetSize( width - 30, 70 )		
				obj.Panel:SetVisible( true )
				
			INV.List:AddItem( obj.Panel )
		end
		
		INV.List:PerformLayout()
		
end

function GM:OnContextMenuOpen()
	INV.Window:SetVisible( true )
end

function GM:OnContextMenuClose()
	INV.Window:SetVisible( false )
end

function GM:OnSpawnMenuOpen()
end

function GM:OnSpawnMenuClose()
end




